var Plot = Plot || {};

Plot.prototype.addChromaticityDiagram = function (diagram, args) {
    'use strict';

    var param = {
        "Planckian locus": true,
        "Daylight locus": true,
        "Spectrum locus": true,
        "Standards illuminants": true,
        "Gamut": true
    };

    var i;
    for (i in args) {
        if (args.hasOwnProperty(i)) {
            param[i] = args[i];
        }
    }

    if (diagram === 'rgY') {
        this.setTitle('CIE rg Diagram', 'r chromaticity', 'g chromaticity');
    } else if (diagram === 'xyY') {
        this.setTitle('CIE xy Diagram', 'x chromaticity', 'y chromaticity');
    } else if (diagram === '1960 uvY') {
        this.setTitle('CIE 1960 uv Diagram', 'u chromaticity', 'v chromaticity');
    } else if (diagram === "1976 u'v'Y") {
        this.setTitle("CIE 1976 u'v' Diagram", "u' chromaticity", "v' chromaticity");
    } else {
        throw new Error('Plot.drawChromaticityDiagram: ' +
                        diagram + ' is not a chromaticity diagram.');
    }

    // Plot properties
    var pLProperties = {
        'id': 'Planckian locus',
        'stroke': 'green',
        'stroke-width': 2
    };
    var sLProperties = {
        'id': 'Spectrum locus',
	'stroke': 'lightseagreen',
	'stroke-width': 2,
	'stroke-dasharray': "5 2"
    };
    var pGProperties = {
        'id': 'Gamut',
        'stroke': 'red',
        'stroke-width': 2
    };
    var sIProperties = {
        'id': 'Standards illuminants',
	'stroke': 'none',
	'fill': 'none',
        'marker': {
            'shape': 'circle',
            'fill': 'orange',
            'size': 2,
            'stroke': 'none'
        }
    };

    // Get primaries Data
    var prim = Colorspaces.CIE.getPrimaries('current', diagram);

    // Get standards illuminants data
    var stdIll = Colorspaces.CIE.getIlluminantList();
    var xStdIll = [], yStdIll = [];

    var i;
    for (i = stdIll.length; i--; i) {
        var ill = Colorspaces.CIE.getIlluminant(stdIll[i], diagram);
        xStdIll.push(ill[0]);
        yStdIll.push(ill[1]);
    }

    // Plot spectrum locus
    if (param["Spectrum locus"] === true) {
        var sL = Colorspaces.CIE.getSpectrumLocus(diagram);
        this.addPath(sL[0], sL[1], sLProperties);
    }

    // Plot planckian locus
    if (param["Planckian locus"] === true) {
        var pL = Colorspaces.CIE.getPlanckianLocus(diagram);
        this.addPath(pL[0], pL[1], pLProperties);
    }

    // Plot primaries gamut
    if (param["Gamut"] === true) {
        var xPrim = [prim.R[0], prim.G[0], prim.B[0], prim.R[0]];
        var yPrim = [prim.R[1], prim.G[1], prim.B[1], prim.R[1]];
        this.addPath(xPrim, yPrim, pGProperties);
    }

    // Plot standards illuminants
    if (param["Standards illuminants"] === true) {
        this.addPath(xStdIll, yStdIll, sIProperties);
    }

    return this;
};

Plot.prototype.properties.chromaticityPath = {'id': 'scatter',
                                              'fill': 'none',
                                              'stroke': 'none',
                                              'marker': {'shape': 'circle',
                                                         'size': 0.25,
                                                         'fill': 'blue'}
                                             };

Plot.prototype.addChromaticitiesFromRgb = function (r, g, b, args, diagram) {
    'use strict';
    diagram = diagram || 'xyY';

    var defaultArgs = this.getProperties('chromaticityPath');
    var i, end;
    for (i in args) {
        if (args.hasOwnProperty(i)) {
            defaultArgs[i] = args[i];
        }
    }
    var convert = Colorspaces.getConversionFunction('RGB to ' + diagram);

    var x = new Float32Array(r.length);
    var y = new Float32Array(r.length);
    for (i = 0, end = r.length; i < end; i++) {
        var tmp = convert([r[i], g[i], b[i]]);
        x[i] = tmp[0];
        y[i] = tmp[1];
    }
    this.addPath(x, y, defaultArgs);
    return this;
};
